package com.example.module_mine.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.module_mine.R
import com.example.module_mine.databinding.ItemReviewBinding


class ReviewAdapter() :
    ListAdapter<ReviewItem, ReviewAdapter.ReviewViewHolder>(DiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReviewViewHolder {
        val binding = ItemReviewBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ReviewViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ReviewViewHolder, position: Int) {
        val currentItem = getItem(position)
        holder.bind(currentItem)
    }

    inner class ReviewViewHolder(private val binding: ItemReviewBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.approveButton.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    val item = getItem(position)
//                    listener.onApproveClick(item)
                }
            }

            binding.rejectButton.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    val item = getItem(position)
//                    listener.onRejectClick(item)
                }
            }
        }

        fun bind(item: ReviewItem) {
            binding.title.text = item.title
            binding.detail.text = item.detail
            binding.statusTag.text = item.status
            binding.statusTag.setBackgroundResource(
                when (item.status) {
                    "待审核" -> R.drawable.bg_pending
                    "已通过" -> R.drawable.bg_approved
                    "已拒绝" -> R.drawable.bg_rejected
                    else -> R.drawable.bg_default
                }
            )

            // 根据状态显示或隐藏按钮
            binding.approveButton.visibility =
                if (item.status == "待审核") View.VISIBLE else View.GONE
            binding.rejectButton.visibility =
                if (item.status == "待审核") View.VISIBLE else View.GONE
        }
    }

    interface OnItemClickListener {
        fun onApproveClick(item: ReviewItem)
        fun onRejectClick(item: ReviewItem)
    }

    class DiffCallback : DiffUtil.ItemCallback<ReviewItem>() {
        override fun areItemsTheSame(oldItem: ReviewItem, newItem: ReviewItem): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: ReviewItem, newItem: ReviewItem): Boolean {
            return oldItem == newItem
        }
    }

    fun setData(newData: List<ReviewItem>) {
        submitList(newData)
    }
}

data class ReviewItem(
    val id: Int,
    val title: String,
    val detail: String,
    val status: String
)